<?php
	include_once('dbcon.php');
	include_once('constants.php');
	include_once('error_log_db.php');
	include_once('general_functions_db.php');
	
	function get_best_author_db($limit, $user_id) {
		global $mysqli;

		$ranking = array();
		
		$ranking_query = $mysqli->query("SELECT user_id, points, first_name, last_name, user_id  FROM users WHERE user_id<>-1 && user_id<>0 && hide_at_ranking <> 1 ORDER BY points DESC, user_id LIMIT ".$limit);
		logIfError($mysqli, $user_id, "authors_ranking_db", "get_best_author_db", "1");

		while ($row = $ranking_query->fetch_assoc()) {
			$author = array('user_id' => $row['user_id'],
							'points' => $row['points'],
							'first_name' => $row['first_name'],
							'last_name' => $row['last_name'],
							'rank' => get_user_rank($row['user_id']));
							
			array_push($ranking, $author);
		}

		return json_encode($ranking, JSON_HEX_QUOT | JSON_HEX_APOS);
	}

	if ( isset($_REQUEST['limit']) && isset($_REQUEST['user_id']) ) {
		echo get_best_author_db($_REQUEST['limit'], $_REQUEST['user_id']);
	}
	
?>
